<template>
  <div>
    <a-form-model ref="form" :model="model" :rules="validatorRules" slot="detail">
      <a-row>
        <a-col :span="12" v-if="false">
          <a-form-model-item label="工艺流程编码" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="serial">
            <a-input v-model="model.serial" placeholder="请输入工艺流程编码"></a-input>
          </a-form-model-item>
        </a-col>
        <a-col :span="12" v-if="false">
          <a-form-model-item label="物料id" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="materialId">
            <a-input v-model="model.materialId" placeholder="请输入物料id"></a-input>
          </a-form-model-item>
        </a-col>
        <a-col :span="12">
          <a-form-model-item label="物料代码" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="materialCode">
            <HfMaterialSearchSelectTag
              v-model="model.materialCode"
              :material-field="['materialCode', 'materialName', 'sap']"
              @change="onMaterialChange(arguments, model)"
            ></HfMaterialSearchSelectTag>
          </a-form-model-item>
        </a-col>
        <a-col :span="12">
          <a-form-model-item label="物料名称" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="materialName">
            <a-input v-model="model.materialName" placeholder="请输入物料名称" disabled></a-input>
          </a-form-model-item>
        </a-col>
        <a-col :span="12">
          <a-form-model-item label="规格型号" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="sap">
            <a-input v-model="model.sap" placeholder="请输入规格型号" disabled></a-input>
          </a-form-model-item>
        </a-col>
        <a-col :span="12">
          <a-form-model-item label="单位" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="unit">
            <j-dict-select-tag
              dict-code="hf_material_unit,material_unit_name,id"
              v-model="model.unit"
              placeholder="请输入单位"
              disabled
            ></j-dict-select-tag>
          </a-form-model-item>
        </a-col>
        <a-col :span="12">
          <a-form-model-item label="物料类型" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="materialType">
            <j-dict-select-tag
              dict-code="hf_material_type,material_type_name,id"
              v-model="model.materialType"
              placeholder="请输入物料类型"
              disabled
            ></j-dict-select-tag>
          </a-form-model-item>
        </a-col>
        <a-col :span="12">
          <a-form-model-item label="工艺流程名" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="name">
            <a-input v-model="model.name" placeholder="请输入工序名"></a-input>
          </a-form-model-item>
        </a-col>
        <a-col :span="12">
          <a-form-model-item label="工艺次序" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="stepOrder">
            <j-dict-select-tag
              v-model="model.stepOrder"
              dict-code="hf_step_order"
              placeholder="请输入工序名"
            ></j-dict-select-tag>
          </a-form-model-item>
        </a-col>
        <a-col :span="12">
          <a-form-model-item label="备注" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="remark">
            <a-textarea v-model="model.remark" placeholder="请输入备注" style="width: 100%;"></a-textarea>
          </a-form-model-item>
        </a-col>
      </a-row>
    </a-form-model>
  </div>
</template>

<script>
import { httpAction } from '@/api/manage'
import { HfIODetailMixin } from '@/mixins/HfIODetailMixin'
import HfMaterialSearchSelectTag from '@comp/HfManual/HfMaterialSearchSelectTag.vue'

export default {
  name: 'HfRoutingForm',
  components: { HfMaterialSearchSelectTag },
  mixins: [HfIODetailMixin],
  data() {
    return {
      model: {},
      bomObj: {},
      labelCol: {
        xs: { span: 24 },
        sm: { span: 6 }
      },
      wrapperCol: {
        xs: { span: 24 },
        sm: { span: 16 }
      },
      confirmLoading: false,
      validatorRules: {
        materialCode: [
          {
            required: true,
            message: '请输入物料代码!',
            validator: (rule, value, callback) => {
              if (!this.model.materialCode) {
                callback(new Error('请输入物料代码!'))
              } else {
                callback()
              }
            }
          }
        ],
        name: [{ required: true, message: '请输入工序名!' }],
        steps: [
          {
            required: true,
            message: '请输入工序步骤!',
            validator: (rule, value, callback) => {
              if (!this.model.steps) {
                callback(new Error('请输入工序步骤!'))
              } else {
                callback()
              }
            }
          }
        ]
      },
      url: {
        add: '/production/hfRouting/add',
        edit: '/production/hfRouting/edit',
        queryById: '/production/hfRouting/queryById'
      }
    }
  },
  computed: {
    formDisabled() {
      return this.disabled
    }
  },
  created() {
    //备份model原始值
    this.modelDefault = JSON.parse(JSON.stringify(this.model))
  },
  mounted() {},
  methods: {
    add() {
      this.modelDefault.steps = [
        {
          bomId: ''
        }
      ]
      this.edit(this.modelDefault)
    },
    edit(record, detailData) {
      this.model = Object.assign({}, record)

      this.selectValue = record.materialCode

      this.visible = true
    },
    submitForm() {
      const that = this
      // 触发表单验证
      this.$refs.form.validate(valid => {
        if (valid) {
          that.confirmLoading = true
          let httpurl = ''
          let method = ''
          if (!this.model.id) {
            httpurl += this.url.add
            method = 'post'
          } else {
            httpurl += this.url.edit
            method = 'put'
          }
          httpAction(httpurl, this.model, method)
            .then(res => {
              if (res.success) {
                that.$message.success(res.message)
                that.$emit('ok')
              } else {
                that.$message.warning(res.message)
              }
            })
            .finally(() => {
              that.confirmLoading = false
            })
        }
      })
    },
    onMaterialChange([, record], tableRow) {
      Object.assign(tableRow, {
        materialId: record.materialId,
        materialCode: record.materialCode,
        materialName: record.materialName,
        sap: record.sap,
        unit: record.unit,
        materialType: record.materialType
      })
    },
    removeStep(index) {
      if (this.model.steps.length <= 1) {
        this.$message.warn('必须要有一个步骤')
        return
      }
      this.model.steps.splice(index, 1)
    },
    addStep() {
      this.model.steps.push({
        bomId: ''
      })
    },
    handleStepChange(args, index) {
      const stepName = args[1]
      const target = this.model.steps[index]
      target.step_dictText = stepName
    },
    handleBomChange([, bomObj], index) {
      // const bomObj = args[1]
      // this.bomObj = bomObj
      // const target = this.model.steps[index]
      // target.materialCode = bomObj.materialCode
      // target.bomId = bomObj.id
      // target.sap = bomObj.sap
      this.bomObj = bomObj
      Object.assign(this.model.steps[index], {
        materialCode: bomObj.materialCode,
        bomId: bomObj.id,
        sap: bomObj.sap
      })
    }
  }
}
</script>
